package com.exrobot.module.rbt.controller.admin.robotinfo;

import com.exrobot.module.system.controller.admin.user.vo.user.UserUpdateStatusReqVO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.exrobot.framework.common.pojo.PageParam;
import com.exrobot.framework.common.pojo.PageResult;
import com.exrobot.framework.common.pojo.CommonResult;
import com.exrobot.framework.common.util.object.BeanUtils;
import static com.exrobot.framework.common.pojo.CommonResult.success;

import com.exrobot.framework.excel.core.util.ExcelUtils;

import com.exrobot.framework.apilog.core.annotation.ApiAccessLog;
import static com.exrobot.framework.apilog.core.enums.OperateTypeEnum.*;

import com.exrobot.module.rbt.controller.admin.robotinfo.vo.*;
import com.exrobot.module.rbt.dal.dataobject.robotinfo.RobotInfoDO;
import com.exrobot.module.rbt.service.robotinfo.RobotInfoService;

@Tag(name = "管理后台 - 餐车和盒配置")
@RestController
@RequestMapping("/rbt/robot-info")
@Validated
public class RobotInfoController {

    @Resource
    private RobotInfoService robotInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建机器人信息")
    @PreAuthorize("@ss.hasPermission('rbt:robot-info:create')")
    public CommonResult<Long> createRobotInfo(@Valid @RequestBody RobotInfoSaveReqVO createReqVO) {
        return success(robotInfoService.createRobotInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新机器人信息")
    @PreAuthorize("@ss.hasPermission('rbt:robot-info:update')")
    public CommonResult<Boolean> updateRobotInfo(@Valid @RequestBody RobotInfoSaveReqVO updateReqVO) {
        robotInfoService.updateRobotInfo(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "修改机器人状态")
    @PreAuthorize("@ss.hasPermission('rbt:robot-info:update')")
    public CommonResult<Boolean> updateUserStatus(@Valid @RequestBody RobotInfoSaveStatusReqVO reqVO) {
        robotInfoService.updateUserStatus(reqVO.getId(), reqVO.getStatus());
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除机器人信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rbt:robot-info:delete')")
    public CommonResult<Boolean> deleteRobotInfo(@RequestParam("id") Long id) {
        robotInfoService.deleteRobotInfo(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除机器人信息")
                @PreAuthorize("@ss.hasPermission('rbt:robot-info:delete')")
    public CommonResult<Boolean> deleteRobotInfoList(@RequestParam("ids") List<Long> ids) {
        robotInfoService.deleteRobotInfoListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得机器人信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rbt:robot-info:query')")
    public CommonResult<RobotInfoRespVO> getRobotInfo(@RequestParam("id") Long id) {
        RobotInfoDO robotInfo = robotInfoService.getRobotInfo(id);
        return success(BeanUtils.toBean(robotInfo, RobotInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得机器人信息分页")
    @PreAuthorize("@ss.hasPermission('rbt:robot-info:query')")
    public CommonResult<PageResult<RobotInfoRespVO>> getRobotInfoPage(@Valid RobotInfoPageReqVO pageReqVO) {
        PageResult<RobotInfoDO> pageResult = robotInfoService.getRobotInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RobotInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出机器人信息 Excel")
    @PreAuthorize("@ss.hasPermission('rbt:robot-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRobotInfoExcel(@Valid RobotInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RobotInfoDO> list = robotInfoService.getRobotInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "机器人信息.xls", "数据", RobotInfoRespVO.class,
                        BeanUtils.toBean(list, RobotInfoRespVO.class));
    }

}